<?php
namespace app\admin\controller;
use think\Config;
use think\Session;
use think\Db;

class Subject extends Base
{
    /**
     * 数据展示
     * @return html
     */
    public function index(){
        $where = array();
        // 查询状态为1的用户数据 并且每页显示10条数据
        $list = db("eb_special_subject")->where($where)->paginate( 5);
        $data  = $list->items();
        foreach($data as $key => $val)
        {
            $data[$key]["grade_id"] = db('eb_grade')->where(["id"=>$val["grade_id"]])->value("name");
        }
        $this->assign('data',$data);
        $this->assign('list',$list);
        $cate=\db('eb_grade')->select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    
    /**
     * 添加页面
     * @return html
     */
    public function add(){
        $list=\db('eb_special_subject')->select();
        $this->assign('list',$list);
        $cate=\db('eb_grade')->select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }

    public function toadd(){
            $data = input();
            $classmodel = model('TwoClassify');
            $list = $classmodel->creat($data);
            if($list){
                $this->success('添加成功',url('subject/index'));
            }else{
                $this->success('添加失败');
            }
    }

    /**
     * 修改分类
     * @return html
     */
    public function edit($id){
        $find  = db( 'eb_special_subject' )->where( ['id'=>$id] )->find();
        $this->assign( 'find', $find );
        $cate=\db('eb_grade')->select();
        $this->assign('cate',$cate);
            return $this->fetch();
    }
    public function update($id){
        $inputdata = input();
        $data = array();
        $data['grade_id'] = $inputdata['grade_id'];
        $data['class_name'] = $inputdata['class_name'];
        $data['pic'] = $inputdata['pic'];
        $data['sort'] = $inputdata['sort'];
        $data['is_show'] = $inputdata['is_show'];
        $Classify = model('TwoClassify');
        $res = $Classify->edit( $id,$data );
        if( $res ) {
            $this->success( '编辑成功', url( 'subject/index' ) );
        } else {
            $this->error( '编辑失败');
        }
    }
     /**
     * 上传图片
     * @return html
     */

    public function upload(){
         // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( './upload/');
        if($info){
            $path = "http://jiaoyu.com/".'./upload/'.$info->getSaveName();
            $res = [];
            $res['code']=1;
            $res['data'] = $path;
            return json($res);
        }else{
            $res = [];
            $res['code']=0;
            return json($res);   
        }
    }

    /**
     * 删除分类
     * @return html
     */
    public function remove($id){
        $Classify = model('TwoClassify');
        $res = $Classify->del( $id );
        if($res){
            $this->success('删除成功' , url("subject/index"));
        } else {
            $this->error('删除失败');
        }
    }
}


